package cn.sumpu.app.guoguojie.web.mvc.misc;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.sumpu.app.guoguojie.biz.AlbumBiz;
import cn.sumpu.app.guoguojie.biz.IdentifierBiz;
import cn.sumpu.app.guoguojie.biz.CmsContentBiz;
import cn.sumpu.app.guoguojie.biz.MemberBiz;
import cn.sumpu.app.guoguojie.biz.MessageBiz;
import cn.sumpu.app.guoguojie.biz.ProductBiz;
import cn.sumpu.app.guoguojie.biz.ProductCategoryBiz;
import cn.sumpu.app.guoguojie.constant.PictureType;
import cn.sumpu.app.guoguojie.domain.CmsContent;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.domain.Order;
import cn.sumpu.app.guoguojie.domain.OrderProduct;
import cn.sumpu.app.guoguojie.domain.Product;
import cn.sumpu.app.guoguojie.domain.ProductCategory;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.IdGenerator;
import cn.sumpu.app.guoguojie.util.OrderStatus;
import cn.sumpu.app.guoguojie.util.PageModel;
import cn.sumpu.app.guoguojie.util.PictureUrl;


@Controller
public class ProductController {

	@Autowired
	private ProductBiz productBiz;
	@Autowired
	private AlbumBiz albumBiz;
	@Autowired
	private MemberBiz memberBiz;
	@Autowired
	private ProductCategoryBiz productCategoryBiz;
	@Resource
	private CmsContentBiz cmsContentBiz;
	@Autowired
	private IdentifierBiz identifierBiz;
	@Autowired
	private MessageBiz messageBiz;
	//上传图片
	@RequestMapping(value = "/misc/product/productUploadImg.htm")
	public String productImgUpload(HttpServletRequest request,HttpServletResponse response) {
		response.setContentType("text/html; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter pw = null;
		try {
			PictureUrl imageUrl = albumBiz.uploadImage(request,PictureType.PRODUCT.getValue(), false);
			pw = response.getWriter();
			pw.print(JSONObject.fromObject(imageUrl));
		} catch (Exception e) {
			e.printStackTrace();
			try {
				pw = response.getWriter();
				String outputJson = "{'err':'上传图片出错！'}";
				pw.print(JSONObject.fromObject(outputJson));
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}
	
	// 产品后台管理
	@RequestMapping(value = "/misc/product/productMain.htm")
	public String rushMain(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		return "misc/product/productMain";
	}
	//查看产品
	@RequestMapping(value = "/misc/product/readProduct.htm")
	public String readProduct(Integer page,HttpServletRequest request,HttpServletResponse response) throws IOException {
		if (page == null) {
			page = 1;
		}
		int pageSize =7;
		int startId = (page - 1) * pageSize;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", startId);
		map.put("pageSize", pageSize);
		try { 
			List<Product> productList =productBiz.selectProductList(map);
			Integer commentCount =productBiz.countProductList(map);
			request.setAttribute("productList", productList);
			PageModel pm = new PageModel();
			pm.setCurrentPage(page);
			pm.setList(productList); 
			pm.setPageSize(pageSize);
			pm.setTotalRecord(commentCount);
			pm.setTotalPage((commentCount + pageSize - 1) / pageSize);
			request.setAttribute("pageModelProduct", pm);
			request.setAttribute("page", page);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "misc/product/productRead";
	}
	//添加产品
	@RequestMapping(value = "/misc/product/addProduct.htm")
	public String addProduct(HttpServletRequest request,HttpServletResponse response) throws IOException { 
		Product product=new Product();
		request.setAttribute("product", product);
		return "misc/product/productAdd";
	}
	//查看订单
	@RequestMapping(value = "/misc/product/readOrder.htm")
	public String readOrder(Integer page,HttpServletRequest request,HttpServletResponse response) throws IOException {
		try {
			if (page == null) {    
				page = 1; 
			} 
			String familyId=null;
			String startDate=null;
			String endDate=null;
			PageModel orderListPageModel =productBiz.selectOrderListPageModel(page,familyId,startDate,endDate);
			request.setAttribute("orderListPageModel", orderListPageModel);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "misc/product/orderRead";
	}
	//添加订单
	@RequestMapping(value = "/misc/product/addOrder.htm")
	public String addOrder(HttpServletRequest request,HttpServletResponse response) throws IOException {
		Order order =new Order();
		request.setAttribute("order", order);
		return "misc/product/orderAdd";
	}
	// 获取产品类目
	@RequestMapping(value = "/misc/product/getProductCategory.htm")
	public void getCategory(HttpServletResponse response) {
		try { 
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			List<ProductCategory> listCategory = productCategoryBiz
					.buildProductCategory();
			out.print(JSONArray.fromObject(listCategory));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//保存产品
	@RequestMapping(value = "/misc/product/submitSaveProduct.htm")
	public String saveProduct(Product product ,HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setContentType("text/plain; charset=utf-8");
		PrintWriter pw = null;
		try {
			product.setIdentifier(identifierBiz.generateId("product")); 
			productBiz.insertNewProduct(product); 
			pw = response.getWriter();
			pw.print("success");
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//删除产品
	@RequestMapping(value = "/misc/product/deleteProduct.htm")
	public String deleteProduct(String identifier,HttpServletRequest request,HttpServletResponse response) throws IOException {
		
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try { 
			productBiz.deleteProductById(identifier);
			pw = response.getWriter();
			pw.print("success");
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return null;
	}
	//更新产品页面
	@RequestMapping(value = "/misc/product/updateProduct.htm")
	public String updateProduct(HttpServletRequest request,String productId,HttpServletResponse response) throws IOException {
		try {
			Product product =productBiz.selectProduct(productId);
			request.setAttribute("product", product);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "misc/product/productAdd";
	}
	//更新产品
	@RequestMapping(value = "/misc/product/updateProductById.htm")
	public String updateProductById(Product product,HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try { 
			productBiz.updateProductById(product);
			pw = response.getWriter();
			pw.print("success");
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return null;
	}
	//保存订单
	@RequestMapping(value = "/misc/product/saveOrder.htm")
	public String saveOrder(OrderProduct orderProduct ,Order order,HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setContentType("text/plain; charset=utf-8");
		PrintWriter pw = null;
		try {  
			//orderProduct.setType("01");
			order.setIdentifier(identifierBiz.generateId("order")); 
			Member member =memberBiz.getMemberByNickname(order.getNickname());
			order.setFamily(member.getFamily());
			order.setMember(member.getIdentifier());
			productBiz.insertOrder(order,orderProduct); 
			pw = response.getWriter();
			pw.print("success");
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return null;
	}
	//更新订单状态
	@RequestMapping(value = "/misc/product/updateOrderStatus.htm")
	public String updateOrderStatus(String identifier,String status,HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {         
			productBiz.updateOrderStatus(identifier,status);
			Order order =productBiz.selectViewOrder(identifier);
			String content=null;
			String word = OrderStatus.orderStatus.get(status);
			content="您的(订单号为："+identifier+")订单状态已更改，订单状态为"+word;
			//messageBiz.insertSystemMessage(order.getFamily(), content);
			pw = response.getWriter();
			pw.print("success");
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return null;
	}
	//查看订单详情
	@RequestMapping(value = "/misc/product/viewOrder.htm")
	public String viewOrder(String orderId,HttpServletRequest request,HttpServletResponse response) throws IOException {
		try { 
			Order orderView =productBiz.selectViewOrder(orderId); 
			request.setAttribute("orderView", orderView);  
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "misc/product/orderView";
	}
	//更新订单页面
	@RequestMapping(value = "/misc/product/updateOrderById.htm")
	public String updateOrderById(HttpServletRequest request,String orderId,HttpServletResponse response) throws IOException {
		try { 
			Order order =productBiz.selectViewOrder(orderId);
			request.setAttribute("order", order);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "misc/product/orderAdd";
	}
	//更新订单
	@RequestMapping(value = "/misc/product/updateOrderByOrderId.htm")
	public String updateOrderByOrderId(Order order,OrderProduct orderProduct,HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try { 
			Member member =memberBiz.getMemberByNickname(order.getNickname());
			order.setFamily(member.getFamily());
			order.setMember(member.getIdentifier());
			productBiz.updateOrderByOrderId(order,orderProduct); 
			pw = response.getWriter();
			pw.print("success");
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return null;
	}
	//今日推荐
	@RequestMapping(value = "/misc/product/recommendProduct.htm")
	public String recommendProduct(String productId,HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try { 
			productBiz.recommendProduct(productId);
			pw = response.getWriter();
			pw.print("success");
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return null;
	}
	//上架
	@RequestMapping(value = "/misc/product/openProduct.htm")
	public String openProduct(String productId,HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try { 
			Product product=productBiz.selectProduct(productId);
			product.setEnabled(true);
			productBiz.updateProductById(product);
			pw = response.getWriter();
			pw.print("success");
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return null;
	}
	//下架
	@RequestMapping(value = "/misc/product/closedProduct.htm")
	public String closedProduct(String productId,HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			Product product=productBiz.selectProduct(productId);
			product.setEnabled(false);
			productBiz.updateProductById(product);
			pw = response.getWriter();
			pw.print("success");
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return null;
	}
	// 加载头部广告位
	@RequestMapping(value = "djzxAlimama1.htm")
	public String djzxAlimama1(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			List<CmsContent> contentList = cmsContentBiz.getContentList(
					"39af1bd1-d915-4d4c-aa7d-229b19ce5ae7", 1);
			request.setAttribute("contentList", contentList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "v2/product/alimama1";
	}
	// 加载右侧广告位
	@RequestMapping(value = "djzxAlimama2.htm")
	public String djzxAlimama2(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			List<CmsContent> contentList = cmsContentBiz.getContentList(
					"08e8d370-1415-450d-9aa9-c11b2032e471", 1);
			request.setAttribute("contentList", contentList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "v2/product/alimama2";
	}
	//加载兑奖攻略
	@RequestMapping(value = "/djzxDuiJiangGongLue.htm")
	public String djzxDuiJiangGongLue(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			List<CmsContent> contentList = cmsContentBiz.getContentList(
					"7d6b08d8-98b0-4852-a5e0-efe0e7419c1a", 4);
			request.setAttribute("contentList", contentList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "v2/product/duiJiangGongLue";
	}
	
}
